#include "piece_gun.hpp"

bool Piece_Gun::move(const Coordinate &goal){
	Coordinate spacing = goal - coor;
	Coordinate add;
	int num = 0;
	if ((spacing.x == 0) ^ (spacing.y == 0)){
		add = (spacing.x == 0)? Coordinate(0, spacing.y / abs(spacing.y))
			: Coordinate(spacing.x / abs(spacing.x), 0);
		for (Coordinate it = coor + add; it != goal; it += add)
			num += pieces[gameMap[it]]->isLife();
		Piece &pie = *pieces[gameMap[goal]];
		if (num == 0 && pie.isLife() == false){
			updateCoor(gameMap, goal);
			return true;
		}
		else if (num == 1 && pie.isLife() == true && pie.getColor()!= color){
			pie.setLife(false);
			updateCoor(gameMap, goal);
			return true;
		}
	}
	return false;
}

void Piece_Gun::init(){
	switch (pt){
		case RED_BIG_GUN1:
			coor.x = 7;
			coor.y = 1;
                        color = RED;
			break;
		case RED_BIG_GUN2:
			coor.x = 7;
			coor.y = 7;
                        color = RED;
			break;
		case BLACK_BIG_GUN1:
			coor.x = 2;
			coor.y = 1;
                        color = BLACK;
			break;
		case BLACK_BIG_GUN2:
			coor.x = 2;
			coor.y = 7;
                        color = BLACK;
			break;
		default:
			break;
	}
	life = true;
}

Piece_Gun::~Piece_Gun(){}
